﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：JQueryGridHandlerBase.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年6月2日星期六
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Json;
using ajayumi.Platform.Core.Security;

namespace ajayumi.Platform.Web.Controls.DataGrid
{
    public class JQueryGridHandlerBase : IHttpHandler
    {
        /// <summary>
        /// 当前页
        /// </summary>
        protected int CurrentPage { get; private set; }
        /// <summary>
        /// 每页记录数
        /// </summary>
        protected int PageSize { get; private set; }
        /// <summary>
        /// 排序列
        /// </summary>
        protected string SortKey { get; private set; }
        /// <summary>
        /// 排序方式
        /// </summary>
        protected string SortMode { get; private set; }
        /// <summary>
        /// 查询条件
        /// </summary>
        protected string WhereStr { get; private set; }

        private readonly static CryptoProvider m_CryptoProvider = ProviderFactory.CreateCryptoProvider();

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            // get the context.Requested page
            this.CurrentPage = ConvertHelper.ToInt(context.Request.QueryString["page"]);
            // get how many rows we want to have into the grid
            this.PageSize = ConvertHelper.ToInt(context.Request.QueryString["rows"]);
            // get index row - i.e. user click to sort
            this.SortKey = context.Request.QueryString["sidx"];
            // get the direction
            this.SortMode = context.Request.QueryString["sord"];

            //string _operator = context.Request.QueryString["operator"];
            string action = context.Request.QueryString["action"];
            Operation op = Operation.None;
            if (!string.IsNullOrEmpty(action))
            {
                op = (Operation)Enum.Parse(typeof(Operation), m_CryptoProvider.Decrypt(action), true);
            }

            string result = string.Empty;
            switch (op)
            {
                case Operation.Create:
                    break;
                case Operation.Read:
                    if (context.Request.QueryString["_search"].Equals("false", StringComparison.CurrentCultureIgnoreCase))
                    {

                    }
                    else
                    {
                        //反序列化json
                        string conditions = HttpUtility.UrlDecode(context.Request.QueryString["filters"].ToString());
                        var mStream = new MemoryStream(Encoding.UTF8.GetBytes(conditions));     //使用Encoding.Default中文时将会乱码
                        DataContractJsonSerializer dcjson = new DataContractJsonSerializer(typeof(JQueryGridFilter));
                        JQueryGridFilter filter = (JQueryGridFilter)dcjson.ReadObject(mStream);

                        this.WhereStr = JQueryGridSopt.Builder(filter);
                    }
                    result = this.Read(this.CurrentPage, this.PageSize);
                    break;
                case Operation.Update:
                    string oper = context.Request.Form["oper"].ToString();
                    switch (oper)
                    {
                        case "add":
                            this.Create();
                            break;
                        case "edit":
                            this.Update();
                            break;
                        case "del":
                            this.Delete(context);
                            break;
                    }
                    break;
                case Operation.Delete:
                    break;
            }
            context.Response.Write(result);

        }


        public virtual void Create()
        { throw new NotSupportedException("请提供该方法的实现"); }

        public virtual string Read(int currentPage, int pageSize)
        { throw new NotSupportedException("请提供该方法的实现"); }


        public virtual void Update()
        { throw new NotSupportedException("请提供该方法的实现"); }

        public virtual void Delete(HttpContext context)
        { throw new NotSupportedException("请提供该方法的实现"); }
    }
}
